package com.shenhan.lock8;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * @author shenhan
 * @create 2020/6/10 15:52
 * @description 8锁，就是关于锁的8个问题
 * 1、标准情况下，两个线程先打印发短信还是打电话？  1、发短信 2、打电话
 * 2、发短信等待4s的情况下，两个线程先打印发短信还是打电话？  1、发短信 2、打电话
 */
public class Test1 {
    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        new Thread(phone::sendMessage, "A").start();
        TimeUnit.SECONDS.sleep(2L);
        new Thread(phone::call, "B").start();
    }
}

class Phone {
    @SneakyThrows
    public synchronized void sendMessage() {
        TimeUnit.SECONDS.sleep(4L);
        System.out.println(Thread.currentThread().getName() + " 发短信");
    }

    public synchronized void call() {
        System.out.println(Thread.currentThread().getName() + " 打电话");
    }
}
